package com.fg.pro.model.exception;



import cn.hutool.core.util.StrUtil;
import com.fg.pro.model.enums.AppHttpCodeEnum;
import com.fg.pro.model.reponse.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

import static com.fg.pro.model.enums.AppHttpCodeEnum.DATA_JSR303;

@Order(1)
@RestControllerAdvice  //控制器增强类
@Slf4j
public class OtherExceptionCatch {

    /**
     * sql 设置唯一的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = DuplicateKeyException.class)
    @ResponseBody
    public ResponseResult exception(DuplicateKeyException e) {
        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());
        if (e.toString().contains("Duplicate")) {
            String str = e.toString().split("entry")[1].split(" ")[1];
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, str + "重复");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
    }

    /**
     * sql 设置唯一的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public ResponseResult exception(SQLIntegrityConstraintViolationException e) {
        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());
        if (e.toString().contains("Duplicate")) {
            String str = e.toString().split("entry")[1].split(" ")[1];
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, str + "重复");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
    }

    /**
     * 处理可控异常  自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public ResponseResult exception(CustomException e) {
        log.error("catch exception:{}", e);
        return ResponseResult.errorResult(e.getAppHttpCodeEnum());
    }

    /**
     * jsr303 校验
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseResult exception(MethodArgumentNotValidException e) {
        log.error("catch exception:{}", e);
        BindingResult bindingResult = e.getBindingResult();
        List<String> list = bindingResult.getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
        String errMessage = StrUtil.join(",", list);
        return ResponseResult.errorResult(DATA_JSR303,errMessage);
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseResult exception(HttpRequestMethodNotSupportedException e) {
        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());
        if (e.toString().contains("method")) {
            String str = e.toString().split("method")[1].split("is")[0];
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, str + "请求方法不正确");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,  "请求方法不正确");
        }
    }

}
